package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltStationDataSievingBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltStationDataSievingVo;
import com.ruoyi.zhgd.domain.NAsphaltStationDataSieving;
import com.ruoyi.zhgd.mapper.NAsphaltStationDataSievingMapper;
import com.ruoyi.zhgd.service.INAsphaltStationDataSievingService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * 沥青拌合站筛孔数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltStationDataSievingServiceImpl implements INAsphaltStationDataSievingService {

    private final NAsphaltStationDataSievingMapper baseMapper;

    /**
     * 查询沥青拌合站筛孔数据
     */
    @Override
    public NAsphaltStationDataSievingVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青拌合站筛孔数据列表
     */
    @Override
    public TableDataInfo<NAsphaltStationDataSievingVo> queryPageList(NAsphaltStationDataSievingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltStationDataSieving> lqw = buildQueryWrapper(bo);
        Page<NAsphaltStationDataSievingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站筛孔数据列表
     */
    @Override
    public List<NAsphaltStationDataSievingVo> queryList(NAsphaltStationDataSievingBo bo) {
        LambdaQueryWrapper<NAsphaltStationDataSieving> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltStationDataSieving> buildQueryWrapper(NAsphaltStationDataSievingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltStationDataSieving> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDataId()), NAsphaltStationDataSieving::getDataId, bo.getDataId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrganizationId()), NAsphaltStationDataSieving::getOrganizationId, bo.getOrganizationId());
        lqw.eq(StringUtils.isNotBlank(bo.getStationId()), NAsphaltStationDataSieving::getStationId, bo.getStationId());
        lqw.eq(StringUtils.isNotBlank(bo.getMatchId()), NAsphaltStationDataSieving::getMatchId, bo.getMatchId());
        lqw.eq(StringUtils.isNotBlank(bo.getGradeId()), NAsphaltStationDataSieving::getGradeId, bo.getGradeId());
        lqw.eq(bo.getDischargeTime() != null, NAsphaltStationDataSieving::getDischargeTime, bo.getDischargeTime());
        lqw.eq(bo.getSieve0d075value() != null, NAsphaltStationDataSieving::getSieve0d075value, bo.getSieve0d075value());
        lqw.eq(bo.getSieve0d075alarm() != null, NAsphaltStationDataSieving::getSieve0d075alarm, bo.getSieve0d075alarm());
        lqw.eq(bo.getSieve0d15value() != null, NAsphaltStationDataSieving::getSieve0d15value, bo.getSieve0d15value());
        lqw.eq(bo.getSieve0d15alarm() != null, NAsphaltStationDataSieving::getSieve0d15alarm, bo.getSieve0d15alarm());
        lqw.eq(bo.getSieve0d3value() != null, NAsphaltStationDataSieving::getSieve0d3value, bo.getSieve0d3value());
        lqw.eq(bo.getSieve0d3alarm() != null, NAsphaltStationDataSieving::getSieve0d3alarm, bo.getSieve0d3alarm());
        lqw.eq(bo.getSieve0d6value() != null, NAsphaltStationDataSieving::getSieve0d6value, bo.getSieve0d6value());
        lqw.eq(bo.getSieve0d6alarm() != null, NAsphaltStationDataSieving::getSieve0d6alarm, bo.getSieve0d6alarm());
        lqw.eq(bo.getSieve1d18value() != null, NAsphaltStationDataSieving::getSieve1d18value, bo.getSieve1d18value());
        lqw.eq(bo.getSieve1d18alarm() != null, NAsphaltStationDataSieving::getSieve1d18alarm, bo.getSieve1d18alarm());
        lqw.eq(bo.getSieve2d36value() != null, NAsphaltStationDataSieving::getSieve2d36value, bo.getSieve2d36value());
        lqw.eq(bo.getSieve2d36alarm() != null, NAsphaltStationDataSieving::getSieve2d36alarm, bo.getSieve2d36alarm());
        lqw.eq(bo.getSieve4d75value() != null, NAsphaltStationDataSieving::getSieve4d75value, bo.getSieve4d75value());
        lqw.eq(bo.getSieve4d75alarm() != null, NAsphaltStationDataSieving::getSieve4d75alarm, bo.getSieve4d75alarm());
        lqw.eq(bo.getSieve9d5value() != null, NAsphaltStationDataSieving::getSieve9d5value, bo.getSieve9d5value());
        lqw.eq(bo.getSieve9d5alarm() != null, NAsphaltStationDataSieving::getSieve9d5alarm, bo.getSieve9d5alarm());
        lqw.eq(bo.getSieve13d2value() != null, NAsphaltStationDataSieving::getSieve13d2value, bo.getSieve13d2value());
        lqw.eq(bo.getSieve13d2alarm() != null, NAsphaltStationDataSieving::getSieve13d2alarm, bo.getSieve13d2alarm());
        lqw.eq(bo.getSieve16value() != null, NAsphaltStationDataSieving::getSieve16value, bo.getSieve16value());
        lqw.eq(bo.getSieve16alarm() != null, NAsphaltStationDataSieving::getSieve16alarm, bo.getSieve16alarm());
        lqw.eq(bo.getSieve19value() != null, NAsphaltStationDataSieving::getSieve19value, bo.getSieve19value());
        lqw.eq(bo.getSieve19alarm() != null, NAsphaltStationDataSieving::getSieve19alarm, bo.getSieve19alarm());
        lqw.eq(bo.getSieve26d5value() != null, NAsphaltStationDataSieving::getSieve26d5value, bo.getSieve26d5value());
        lqw.eq(bo.getSieve26d5alarm() != null, NAsphaltStationDataSieving::getSieve26d5alarm, bo.getSieve26d5alarm());
        lqw.eq(bo.getSieve31d5value() != null, NAsphaltStationDataSieving::getSieve31d5value, bo.getSieve31d5value());
        lqw.eq(bo.getSieve31d5alarm() != null, NAsphaltStationDataSieving::getSieve31d5alarm, bo.getSieve31d5alarm());
        lqw.eq(bo.getAlarm() != null, NAsphaltStationDataSieving::getAlarm, bo.getAlarm());
        return lqw;
    }

    /**
     * 新增沥青拌合站筛孔数据
     */
    @Override
    public Boolean insertByBo(NAsphaltStationDataSievingBo bo) {
        NAsphaltStationDataSieving add = BeanUtil.toBean(bo, NAsphaltStationDataSieving.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站筛孔数据
     */
    @Override
    public Boolean updateByBo(NAsphaltStationDataSievingBo bo) {
        NAsphaltStationDataSieving update = BeanUtil.toBean(bo, NAsphaltStationDataSieving.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltStationDataSieving entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站筛孔数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
